package com.hw.springcloud.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hw.springcloud.dao.OrderDao;
import com.hw.springcloud.entity.OrderEntity;
import com.hw.springcloud.service.OrderAccountService;
import com.hw.springcloud.service.OrderService;
import com.hw.springcloud.service.OrderStorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @Classname: OrderServiceImpl
 * @Description: 描述
 * @Date: 2021/12/21
 * @Author: HuWei
 */
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {

    @Resource
    private OrderAccountService accountService;

    @Resource
    private OrderStorageService orderStorageService;

    @Override
    @GlobalTransactional(name = "order_create",rollbackFor = Exception.class)
    public Boolean createOrder(OrderEntity orderEntity) {
        log.info("storage");
        orderStorageService.updateStorage(orderEntity.getProductId(),orderEntity.getCount());
        log.info("account");
        BigDecimal used = new BigDecimal(orderEntity.getCount()).multiply(orderEntity.getMoney());
        accountService.updateAccount(orderEntity.getUserId(),used);
        log.info("order");
        return this.save(orderEntity);
    }

    @Override
    public Boolean updateOrder(OrderEntity orderEntity) {
        return this.updateById(orderEntity);
    }

    @Override
    public OrderEntity getById(Long id) {
        return this.getById(id);
    }
}
